$(function() {
	var filePath = "";
	$("#goods_list").on("click", ".edit-goods", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'GET',
			url: "http://127.0.0.1:8080/goods/getGoods",
			dataType: 'json',
			data: {
				"id": data.id
			},
			success: function(data) {
				var goods = data.data;
				console.log(goods);
				$("#goods_id").val(goods.id);
				$("#goodsName").val(goods.goodsName);
				filePath = goods.imgUrl;
				$("#specification").val(goods.specification);
				$("#nature").val(goods.nature);
				$("#description").val(goods.description);
				$("#price").val(goods.price);
				$("#repertory").val(goods.repertory);
				$("#boutique").val(goods.boutique);
				$("#classify").val(goods.classify);
				$("#addRowModal").modal('show');
			}
		})
	})

	$("#goods_list").on("click", ".remove-goods", function() {
		var content = {};
		content.title = '警告提示';
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/goods/deleteGoods",
			dataType: 'json',
			data: {
				"id": data.id
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				content.message = data.msg;
				$.notify(content, {
					type: "warning",
					placement: {
						from: "top",
						align: "center"
					},
					time: 2000,
					delay: 1,
				});
				if (data.code == 'success') {
					dataTablesReload.ajax.reload(null, false);
				}
			}
		})
	})

	$("#imgUpload").click(function() {
		var node = $("#imgUrl");
		var fileName = node.val();
		var content = {};
		content.title = '警告提示';
		if (fileName == "") {
			content.message = "请选择上传的图片";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		var formdata = new FormData();
		formdata.append('file', node.get(0).files[0]);
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/goods/imageUpload",
			dataType: 'json',
			data: formdata,
			contentType: false,
			processData: false,
			success: function(data) {
				if (data.code == 'success') {
					filePath = data.data;
				}
				content.message = data.msg;
				$.notify(content, {
					type: "warning",
					placement: {
						from: "top",
						align: "left"
					},
					time: 2000,
					delay: 1,
				});
			},
			error: function(e) {
				content.message = "上传失败";
				$.notify(content, {
					type: "warning",
					placement: {
						from: "top",
						align: "left"
					},
					time: 2000,
					delay: 1,
				});
			}
		})
	})

	$("#addRowButton").click(function() {
		var id = $("#goods_id").val();
		var goodsName = $("#goodsName").val();
		var specification = $("#specification").val();
		var nature = $("#nature").val();
		var description = $("#description").val();
		var price = $("#price").val();
		var repertory = $("#repertory").val();
		var boutique = $("#boutique").val();
		var classify = $("#classify").val();
		var content = {};
		content.title = '警告提示';
		if (goodsName == '') {
			content.message = "请输入商品名称";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		if (specification == '') {
			content.message = "请选择商品规格";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		if (nature == '') {
			content.message = "请选择商品属性";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		if (price == '') {
			content.message = "请选输入商品价格";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		if (repertory == '') {
			content.message = "请选输入商品库存";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		if (filePath == '') {
			content.message = "请上传图片";
			$.notify(content, {
				type: "warning",
				placement: {
					from: "top",
					align: "left"
				},
				time: 2000,
				delay: 1,
			});
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/goods/addOrUpdateGoods",
			dataType: "json",
			data: {
				"goodsName": goodsName,
				"imgUrl": filePath,
				"specification": specification,
				"nature": nature,
				"description": description,
				"price": price,
				"repertory": repertory,
				"id": id,
				"boutique": boutique,
				"classify": classify
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					$("#addRowModal").modal('hide');
					dataTablesReload.ajax.reload(null, false);
				} else {
					content.message = "新增商品失败";
					$.notify(content, {
						type: "warning",
						placement: {
							from: "top",
							align: "left"
						},
						time: 2000,
						delay: 1,
					});
				}
			}
		});
	});

	var dataTablesReload = $('#basic-datatables').DataTable({
		language: {
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sSearch": "搜索:",
			"oPaginate": {
				"sPrevious": "上页",
				"sNext": "下页"
			}
		},
		pageLength: 10,
		pagingType: "simple",
		bFilter: false,
		bLengthChange: true,
		serverSide: true,
		ajax: function(data, callback, settings) {
			var pagesize = data.length;
			var page = (data.start) / data.length + 1;
			var content = {};
			content.title = '警告提示';
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/goods/findGoodsList",
				cache: false,
				data: {
					"pageNumber": page,
					"pageSize": pagesize
				},
				dataType: "json",
				success: function(data) {
					var code = data.code;
					if (code == 'success') {
						var returnData = {};
						returnData.recordsTotal = data.data.count;
						returnData.recordsFiltered = data.data.count;
						returnData.data = data.data.goodsList;
						callback(returnData);
					} else {
						content.message = data.msg;
						$.notify(content, {
							type: "warning",
							placement: {
								from: "top",
								align: "center"
							},
							time: 2000,
							delay: 1,
						});
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					content.message = '获取分页数据失败';
					$.notify(content, {
						type: "warning",
						placement: {
							from: "top",
							align: "center"
						},
						time: 2000,
						delay: 1,
					});
				}
			});
		},
		columns: [{
				"data": "goodsName",
				"defaultContent": "<i></i>"
			},
			{
				"data": "specification",
				"defaultContent": "<i></i>",
				"render": function specificationMap(sp) {
					if (sp == 1) {
						return "全网通版 6GB+64GB";
					} else if (sp == 2) {
						return "全网通版 6GB+128GB";
					}
				}
			},
			{
				"data": "nature",
				"defaultContent": "<i></i>",
				"render": function natureMap(nt) {
					if (nt == 1) {
						return "黑亮色";
					} else if (nt == 2) {
						return "魅力蓝";
					} else if (nt == 3) {
						return "活力红";
					}
				}
			},
			{
				"data": "price",
				"defaultContent": "<i></i>"
			},
			{
				"data": "repertory",
				"defaultContent": "<i></i>"
			}, {
				"data": "",
				"defaultContent": '<div class="form-button-action"><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-primary btn-lg edit-goods" data-original-title="Edit Task">' +
					'<i class="fa fa-edit"></i></button><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-danger remove-goods" data-original-title="Remove"><i class="fa fa-times">' +
					'</i></button></div>'
			},
		]
	});

	$.ajax({
		type: "GET",
		url: "http://127.0.0.1:8080/mgrUser/getMgrSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#user_info");
				node.empty();
				var html = data.data.userName + '<span class="user-level">管理员</span><span class="caret"></span>';
				node.html(html);
			} else {
				window.location.href = './login.html';
			}
		}
	});
})
